package com.quanquan.store.service.marketActivity;

import com.quanquan.store.entity.marketActivity.collectCards.*;
import com.quanquan.store.repository.marketActivity.collectCards.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @ClassName CollectCardsService
 * @Description
 * @Author Owen
 * @Date 2019/9/19 0019 下午 6:07
 * @Version V1.0
 **/
@Service
public class CollectCardsService {

    @Autowired
    private CollectCardsRepository collectCardsRepository;

    @Autowired
    private CollectCardsRecordRepository collectCardsRecordRepository;

    @Autowired
    private CollectCardsCardRepository collectCardsCardRepository;

    @Autowired
    private CollectUserCardRepository collectUserCardRepository;

    @Autowired
    private CollectCardsGiftRepository collectCardsGiftRepository;

    @Autowired
    private CollectCardsGiftRecordRepository collectCardsGiftRecordRepository;

    /**
     * 根据ID查询活动信息
     * @param id
     * @return
     */
    public CollectCards getById(Long id){
        CollectCards collectCards = collectCardsRepository.findById(id).orElse(null);
        return collectCards;
    }

    public List<CollectCardsRecord> getRecordListByConsumId(Long activityId, String consumId){
        List<CollectCardsRecord> all = collectCardsRecordRepository.findAllByActivityIdAndConsumId(activityId, consumId);
        return all;
    }

    public List<CollectCardsCard> getAllCards(Long activityId){
        List<CollectCardsCard> all = collectCardsCardRepository.findAllByActivityIdAndStatusOrderBySortAsc(activityId, 1);
        return all;
    }

    public CollectCardsCard getCardById(Long cardId){
        CollectCardsCard cardsCard = collectCardsCardRepository.findById(cardId).orElse(null);
        return cardsCard;
    }

    public List<CollectCardsRecord> getByUserIdAndCardId(String userId, Long cardId){
        return collectCardsRecordRepository.findAllByUserIdAndCardId(userId,cardId);
    }

    public CollectCardsRecord saveOrUpdateRecord(CollectCardsRecord record){
        return collectCardsRecordRepository.saveAndFlush(record);
    }

    public CollectUserCard getUserCardByCardId(String userId, Long cardId){
        CollectUserCard userCard = collectUserCardRepository.findByUserIdAndCardId(userId, cardId);
        return userCard;
    }

    public CollectUserCard saveOrUpdateUserCard(CollectUserCard userCard){
        return collectUserCardRepository.saveAndFlush(userCard);
    }

    public List<CollectCardsRecord> getUserCardsRecord(Long activityId, String userId, Integer page, Integer size){

        Sort sort = new Sort(Sort.Direction.DESC,"createTime");

        Pageable pageable = PageRequest.of(page,size,sort);

        CollectCardsRecord collectCardsRecord = new CollectCardsRecord();
        collectCardsRecord.setActivityId(activityId);
        collectCardsRecord.setUserId(userId);

        Example<CollectCardsRecord> example = Example.of(collectCardsRecord);

        Page<CollectCardsRecord> all = collectCardsRecordRepository.findAll(example, pageable);
        List<CollectCardsRecord> recordList = all.getContent();
        return recordList;
    }

    public List<CollectCardsGift> getGiftsByActivityIdForGame(Long activityId){
        List<CollectCardsGift> giftList = collectCardsGiftRepository.findAllByActivityIdAndStatusAndStartTimeBeforeAndEndTimeAfterAndRemainTotalGreaterThan(activityId, 1, new Date(), new Date(),0);
        return giftList;
    }


    /**
     * 扣将活动奖项剩余库存
     * @param giftId
     * @return
     */
    public int substractGiftStock(Long giftId){
        int stock = collectCardsGiftRepository.substractStock(giftId);
        return stock;
    }

    public CollectCardsGiftRecord saveOrUpdateGiftRecord(CollectCardsGiftRecord record){
        return collectCardsGiftRecordRepository.saveAndFlush(record);
    }

    public List<CollectCardsGiftRecord> getUserGiftRecord(Long activityId, String userId){
        return collectCardsGiftRecordRepository.findAllByActivityIdAndUserId(activityId,userId);
    }

    public int countUserCards(Long activityId,String userId){
        int count = collectUserCardRepository.countByActivityIdAndUserIdAndQuantityGreaterThan(activityId, userId, 0);
        return count;
    }
}
